//
//  NewsViewController.m
//  老司机发车了
//
//  Created by 斌 on 16/8/5.
//  Copyright © 2016年 斌. All rights reserved.
//

#import "NewsViewController.h"
#import "CustomLabel.h"
#import "FirstNewsTableViewController.h"
#import "EntertainmentTableViewController.h"
#import "StoryTableViewController.h"
#import "MilitaryTableViewController.h"
#import "TechnologyTableViewController.h"

@interface NewsViewController ()<UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *titleScrollView;

@property (weak, nonatomic) IBOutlet UIScrollView *tableScrollView;

@property (nonatomic, strong)NSArray *array;
@end

@implementation NewsViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    self.navigationController.navigationBar.translucent = NO;
    self.array = @[@"头条", @"娱乐", @"故事", @"体育", @"科技"];

    [self addTitle];
    [self addTableView];
    [self scrollViewDidEndDecelerating:self.tableScrollView];

}



//创建标题
- (void)addTitle{

    for (NSInteger i = 0; i < _array.count; i++) {
        CustomLabel *label = [[CustomLabel alloc] init];
        label.text = _array[i];
        label.frame = CGRectMake(100*i, 0, 100, 40);

        if (i == 0) {
            label.scale = 1.0;
        }
        label.tag = i + 100;
        //
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(titleClick:)];
        [label addGestureRecognizer:tap];
        [self.titleScrollView addSubview:label];
    }
    //设置 scrollView 的 contentSize
    self.titleScrollView.contentSize = CGSizeMake(_array.count*100, 0);
    self.tableScrollView.contentSize = CGSizeMake(5*375, 0);

}

//label 的点击方法
- (void)titleClick:(UIGestureRecognizer *)tap{
    //通过 tag 值找到 label
    NSInteger index = tap.view.tag - 100;
    //改变下面的 scrollView 的偏移量
    //首先 先拿到当前偏移量
    CGPoint offset = self.tableScrollView.contentOffset;
    offset.x = index * [UIScreen mainScreen].bounds.size.width;
    //重新赋值偏移量
    [self.tableScrollView setContentOffset:offset animated:YES];
}

//创建 tableView
- (void)addTableView{
    FirstNewsTableViewController *firstTable = [[FirstNewsTableViewController alloc] init];
    [self addChildViewController:firstTable];

    EntertainmentTableViewController *entertainmentTable = [[EntertainmentTableViewController alloc] init];
    [self addChildViewController:entertainmentTable];

    StoryTableViewController *storyTable = [[StoryTableViewController alloc] init];
    [self addChildViewController:storyTable];

    MilitaryTableViewController *militaryTable = [[MilitaryTableViewController alloc] init];
    [self addChildViewController:militaryTable];
    
    TechnologyTableViewController *technologyTable = [[TechnologyTableViewController alloc] init];
    [self addChildViewController:technologyTable];
}

#pragma mark------------scrollView 代理---------------
//相当于持续监控 scrollView 一动就走这个方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //获取偏移比例
    CGFloat scale = scrollView.contentOffset.x / scrollView.frame.size.width;
    //避免数组越界判断
    if (scale < 0 || scale >= self.titleScrollView.subviews.count - 1) {
        return;
    }
    //获得左右两边 label
    NSInteger leftIndex = scale;
    CustomLabel *leftLabel = self.titleScrollView.subviews[leftIndex];

    NSInteger rightIndex = leftIndex + 1;
    CustomLabel *rightLabel = self.titleScrollView.subviews[rightIndex];

    //右边比例(在变大)
    CGFloat rightScale = scale - leftIndex;

    //左边比例(在变小)
    CGFloat leftScale = 1- rightScale;

    leftLabel.scale = leftScale;
    rightLabel.scale = rightScale;


}

//动画执行完之后走的方法
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    //获得 scrollView 在屏幕上的宽度(屏幕的宽度)
    CGFloat width = scrollView.frame.size.width;
    //获得偏移量
    CGFloat offsetX = scrollView.contentOffset.x;
    //获得当前所在索引
    NSInteger  index = offsetX / width;

    //关联 titleScrollView
    //获得 label
    CustomLabel *label = self.titleScrollView.subviews[index];
    //获得 label 的偏移量
    CGPoint offsetLabel = self.titleScrollView.contentOffset;
    //获得 lableScrollView的新偏移量
    offsetLabel.x = label.center.x - width / 2;
    //左边最小偏移量 右边最大偏移量
    if (offsetLabel.x < 0) {
        offsetLabel.x = 0;
    }
    if (offsetLabel.x > self.titleScrollView.contentSize.width - width) {
        offsetLabel.x = self.titleScrollView.contentSize.width - width;
    }
    [self.titleScrollView setContentOffset:offsetLabel animated:YES];


    // 让其他label回到最初的状态
    for (CustomLabel *otherLabel in self.titleScrollView.subviews) {
        if (otherLabel != label) otherLabel.scale = 0.0;
    }
    //取出控制器
    UIViewController *VC = self.childViewControllers[index];
    //判断控制器是否已经加载
    if ([VC isViewLoaded]) {
        return;
    }

    //添加视图控制器的视图到 scrollView 上
    VC.view.frame = CGRectMake(offsetX, 0, width, scrollView.frame.size.height);
    [scrollView addSubview:VC.view];
}
//结束减速
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [self scrollViewDidEndScrollingAnimation:scrollView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
